/*
 * Copyright (c) 2012-2013, CS Bakery. All rights reserved.
 */

package csBakery;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.Vector;

/*
 * 	@release Final Release
 *	@author  Chan Ka Yan
 *	@version 1.0.0
 */

public class CostList{
	Vector<CostItem> vector;
	public static final String defaultLink = "cost.txt";
	private String thisLink;

	public CostList(String link){
		thisLink = link;
		openFile(link);
	}
	public static void clearFile(String link){
		File f = new File(link);
		if (f.exists()){
			try{
				f.delete();
				f.createNewFile();
			} catch (Exception e){
				System.out.println("CostList Error: " + e.getMessage());
			}
		}
	}
	private void openFile(String link){
		try{
			//read cost file from existing cost file
			vector = new Vector(10);
			File f = new File(link);
			if (!f.exists()){
				f.createNewFile(); //new cost file if it doesn't exists
			}
			FileInputStream fstream = new FileInputStream(f);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String str = br.readLine();
			while (str != null){
				//trim the str and add CostItem to vector
				String temp = str;
				int index = temp.indexOf(' ');
				String name = temp.substring(0, index);
				temp = temp.substring(index+1);
				
				//check if the name contain two words
				while (temp.indexOf(' ') != -1){
					index = temp.indexOf(' ');
					name += " " + temp.substring(0, index);
					temp = temp.substring(index+1);
				}
				
				//CostItem itm = new CostItem(name, Integer.parseInt(temp)); //refactored, int -> float
				CostItem itm = new CostItem(name, Float.parseFloat(temp));
				addNewCost(itm);
				str = br.readLine();
			}
			in.close();	 
		} catch (Exception e){
			System.out.println("Error: " + e.getMessage());
		}
	}
	public void addNewCost(CostItem itm){
		vector.add(itm);
	}
	public void addNewCost(String name, float price){
		vector.add(new CostItem(name, price));
	}
	public String toString(){
		//return all the log in one String
		String toString = "";
		for (int i = 0; i < vector.size(); i++){
			toString = new String(toString + vector.elementAt(i).toString() + "\n");
		}
		return toString;
	}
	
	public float getCost(String name){
		for (int i = 0; i < vector.size(); i++){
			CostItem costItem = vector.elementAt(i);
			
			if (costItem.getName().equals(name)){
				return costItem.getCost();
			}
		}
		return 0;
	}
	public class CostItem{
		private String product;
		private float cost;
		public CostItem(String name, float cost){
			this.product = name;
			this.cost = cost;
		}
		
		public String toString(){
			return product + " $" + cost;
		}
		public String getName(){
			return product;
		}
		public float getCost(){
			return cost;
		}
	}
}
